﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.GlobalVariables;

public partial class Index : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //this.Panel1.Enabled = true;
        //this.Panel1.Page.MapPath("Default.apsx");
        
        //this.Panel1.Page.ClientTarget = "Default.aspx";
        if (!this.IsPostBack)
        {
            User user = (User)Session["login"];
            Boolean login = UserManageDAO.IsLogin(user);
            if (login)
            {
                this.correctPassword.Text = user.name;
                this.correctPassword.NavigateUrl = GlobalUser.CORRECTPASSWORD_PAGE;
                this.correctPassword.Target = "mainFrame";
                this.currentDate.Text = DateTime.Today.ToString("yyyy-MM-dd") + "  " + DateTime.Today.DayOfWeek.ToString();
                if (user.role != UserRole.ADMIN)
                {
                    this.staffBtn.Visible = false;
                }
                this.SetNoticeBars();
                //this.SetSingleInputFunctions();
            }
            else
            {
                Response.Write(" <script> top.location = '" + GlobalResource.LOGIN_PAGE + "' </script> ");
                return;
            }
        }

    }

    /// <summary>
    /// 注销用户登录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void logoff_Click(object sender, EventArgs e)
    {
        Session.Clear();
        Response.Redirect(GlobalResource.LOGIN_PAGE);
    }
   
    /// <summary>
    /// 设置单个录入功能栏
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void inputBtn_Click(object sender, EventArgs e)
    {
        SetSingleInputFunctions();
    }

    /// <summary>
    /// 设置单个录入功能列表
    /// </summary>
    private void SetSingleInputFunctions()
    {
        //清空Panel1中的控件
        this.Panel1.Controls.Clear();
        User user = (User)Session["login"];
        //用户未登录则跳转到登录页面
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        //用户权限为空则return
        if (null == user.grantedModulars)
        {
            return;
        }
        
        //将功能模块链接添加到Panel1中
        foreach (FunctionModular temp in user.grantedModulars)
        {
            HyperLink hy = GetFunctionHyperLink(temp);
            if (null != hy)
            {
                this.Panel1.Controls.Add(hy);
            }
        }

        if (this.Panel1.Controls.Count != 0)
        {
            HyperLink hy = (HyperLink)this.Panel1.Controls[0];
            this.mainFrame.Attributes.Add("src", hy.NavigateUrl);
        }
        else
        {
            this.mainFrame.Attributes.Add("src", "");
        }
    }

    /// <summary>
    /// 获取功能模块对应的超链接
    /// </summary>
    /// <param name="func">功能模块参数</param>
    /// <returns>返回其对应的超链接</returns>
    private HyperLink GetFunctionHyperLink(FunctionModular func)
    {
        HyperLink hy = new HyperLink();
        hy.Target = "mainFrame";
        hy.Height = Unit.Pixel(25);
        hy.Width = Unit.Percentage(100);
        hy.Font.Underline = false;
        hy.Style.Value = "text-align:center;text-valign:middle";
        //中文期刊
        if (func.id.Equals("zhongwenqikan"))
        {
            hy.NavigateUrl = GlobalZWQK.INDEX_PAGE;
            hy.Text = "中文期刊";
            return hy;
        }
        //外文期刊
        if (func.id.Equals("waiwenqikan"))
        {
            hy.NavigateUrl = GlobalWWQK.INDEX_PAGE;
            hy.Text = "外文期刊";
            return hy;
        }
        //中文会议
        if (func.id.Equals("zhongwenhuiyi"))
        {
            hy.NavigateUrl = GlobalZWHY.INDEX_PAGE;
            hy.Text = "中文会议";
            return hy;
        }
        //外文会议
        if (func.id.Equals("waiwenhuiyi"))
        {
            hy.NavigateUrl = GlobalWWHY.INDEX_PAGE;
            hy.Text = "外文会议";
            return hy;
        }
        //国电法规
        if (func.id.Equals("guodianfagui"))
        {
            hy.NavigateUrl = GlobalGDFG.INDEX_PAGE;
            hy.Text = "国电法规";
            return hy;
        }
        //国家标准
        if (func.id.Equals("guojiabiaozhun"))
        {
            hy.NavigateUrl = GlobalGJBZ.INDEX_PAGE;
            hy.Text = "国家标准";
            return hy;
        }
        //国际标准
        if (func.id.Equals("guojibiaozhun"))
        {
            hy.NavigateUrl = GlobalGJIBZ.INDEX_PAGE;
            hy.Text = "国际标准";
            return hy;
        }
        //行业标准
        if (func.id.Equals("hangyebiaozhun"))
        {
            hy.NavigateUrl = GlobalHYBZ.INDEX_PAGE;
            hy.Text = "行业标准";
            return hy;
        }
        //考察报告
        if (func.id.Equals("kaochabaogao"))
        {
            hy.NavigateUrl = GlobalKCBG.INDEX_PAGE;
            hy.Text = "考察报告";
            return hy;
        }
        //国研报告
        if (func.id.Equals("guoyanbaogao"))
        {
            hy.NavigateUrl = GlobalGYBG.INDEX_PAGE;
            hy.Text = "国研报告";
            return hy;
        }
        //科技报告
        if (func.id.Equals("kejibaogao"))
        {
            hy.NavigateUrl = GlobalKJBG.INDEX_PAGE;
            hy.Text = "科技报告";
            return hy;
        }
        //能源报告
        if (func.id.Equals("nengyuanbaogao"))
        {
            hy.NavigateUrl = GlobalNYBG.INDEX_PAGE;
            hy.Text = "能源报告";
            return hy;
        }
        //学位论文
        if (func.id.Equals("xueweilunwen"))
        {
            hy.NavigateUrl = GlobalXWLW.INDEX_PAGE;
            hy.Text = "学位论文";
            return hy;
        }
        //中国成果
        if (func.id.Equals("zhongguochengguo"))
        {
            hy.NavigateUrl = GlobalZGCG.INDEX_PAGE;
            hy.Text = "中国成果";
            return hy;
        }
        //科技成果
        if (func.id.Equals("kejichengguo"))
        {
            hy.NavigateUrl = GlobalKJCG.INDEX_PAGE;
            hy.Text = "科技成果";
            return hy;
        }
        //自产文献
        if (func.id.Equals("zichanwenxian"))
        {
            hy.NavigateUrl = GlobalZCWX.INDEX_PAGE;
            hy.Text = "自产文献";
            return hy;
        }
        //电力报刊
        if (func.id.Equals("dianlibaokan"))
        {
            hy.NavigateUrl = GlobalDLBK.INDEX_PAGE;
            hy.Text = "电力报刊";
            return hy;
        }
        //科技报刊
        if (func.id.Equals("kejibaokan"))
        {

            hy.NavigateUrl = GlobalKJBK.INDEX_PAGE;
            hy.Text = "科技报刊";
            return hy;
        }
        //经济报刊
        if (func.id.Equals("jingjibaokan"))
        {
            hy.NavigateUrl = GlobalJJBK.INDEX_PAGE;
            hy.Text = "经济报刊";
            return hy;
        }
        //综合报刊
        if (func.id.Equals("zonghebaokan"))
        {
            hy.NavigateUrl = GlobalZHBK.INDEX_PAGE;
            hy.Text = "综合报刊";
            return hy;
        }
        //图书手册
        if (func.id.Equals("tushushouce"))
        {
            hy.NavigateUrl = GlobalTSSC.INDEX_PAGE;
            hy.Text = "图书手册";
            return hy;
        }

        //都没有找到就返回null
        return null;
    }
    /// <summary>
    /// 设置功能权限
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void staffBtn_Click(object sender, EventArgs e)
    {
        this.SetStaffToolbar();
    }

    /// <summary>
    /// 设置权限管理工具栏
    /// </summary>
    private void SetStaffToolbar()
    {
        this.mainFrame.Attributes.Add("src", GlobalUser.LISTUSER_PAGE);
        //清空Panel1中的控件
        this.Panel1.Controls.Clear();

        //设置用户列表链接
        HyperLink hy = new HyperLink();
        hy.NavigateUrl = GlobalUser.LISTUSER_PAGE;
        hy.Target = "mainFrame";
        hy.Text = "用户列表";
        hy.Height = Unit.Pixel(25);
        hy.Width = Unit.Percentage(100);
        hy.Font.Underline = false;
        hy.Style.Value = "text-align:center;text-valign:middle";
        this.Panel1.Controls.Add(hy);

        hy = new HyperLink();
        hy.NavigateUrl = GlobalUser.ADDUSER_PAGE;
        hy.Target = "mainFrame";
        hy.Text = "添加用户";
        hy.Height = Unit.Pixel(25);
        hy.Width = Unit.Percentage(100);
        hy.Font.Underline = false;
        hy.Style.Value = "text-align:center;text-valign:middle";
        this.Panel1.Controls.Add(hy);
    }

    /// <summary>
    /// 查询统计事件处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void countBtn_Click(object sender, EventArgs e)
    {
        this.mainFrame.Attributes.Add("src", GlobalStatistics.STATISTICS_PAGE);
        //清空Panel1中的控件
        this.Panel1.Controls.Clear();

        //设置查询统计链接
        HyperLink hy = new HyperLink();
        hy.NavigateUrl = GlobalStatistics.STATISTICS_PAGE;
        hy.Target = "mainFrame";
        hy.Text = "查询统计";
        hy.Height = Unit.Pixel(25);
        hy.Width = Unit.Percentage(100);
        hy.Font.Underline = false;
        hy.Style.Value = "text-align:center;text-valign:middle";
        this.Panel1.Controls.Add(hy);

        this.Panel1.Controls.Add(hy);

    }

    protected void journalBtn_Click(object sender, EventArgs e)
    {
        this.mainFrame.Attributes.Add("src", GlobalDailyRecord.INDEX_PAGE);
        //清空Panel1中的控件
        this.Panel1.Controls.Clear();

        //设置日志管理链接
        HyperLink hy = new HyperLink();
        hy.NavigateUrl = GlobalDailyRecord.INDEX_PAGE;
        hy.Target = "mainFrame";
        hy.Text = "日志管理";
        hy.Height = Unit.Pixel(25);
        hy.Width = Unit.Percentage(100);
        hy.Font.Underline = false;
        hy.Style.Value = "text-align:center;text-valign:middle";
        this.Panel1.Controls.Add(hy);

        this.Panel1.Controls.Add(hy);
    }

    /// <summary>
    /// 通知
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void noticeBtn_Click(object sender, EventArgs e)
    {
        this.SetNoticeBars();
    }

    /// <summary>
    /// 设置文章列表
    /// </summary>
    private void SetNoticeBars()
    {
        User user = (User)Session["login"];
        //用户未登录则跳转到登录页面
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        this.mainFrame.Attributes.Add("src", GlobalNOTICE.LISTPM_PAGE);
        //清空Panel1中的控件
        this.Panel1.Controls.Clear();

        //设置用户列表链接
        HyperLink hy = new HyperLink();
        hy.NavigateUrl = GlobalNOTICE.LISTPM_PAGE;
        hy.Target = "mainFrame";
        hy.Text = "通知列表";
        hy.Height = Unit.Pixel(25);
        hy.Width = Unit.Percentage(100);
        hy.Font.Underline = false;
        hy.Style.Value = "text-align:center;text-valign:middle";
        this.Panel1.Controls.Add(hy);

        if (user.role == UserRole.ADMIN)
        {
            hy = new HyperLink();
            hy.NavigateUrl = GlobalNOTICE.ADDPM_PAGE;
            hy.Target = "mainFrame";
            hy.Text = "新增通知";
            hy.Height = Unit.Pixel(25);
            hy.Width = Unit.Percentage(100);
            hy.Font.Underline = false;
            hy.Style.Value = "text-align:center;text-valign:middle";
            this.Panel1.Controls.Add(hy);
        }
    }
}